package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDTO;
import com.heima.model.admin.dtos.PageResponseResult;
import com.heima.model.admin.dtos.ResponseResult;
import com.heima.model.admin.enums.AppHttpCodeEnum;
import com.heima.model.admin.pojos.AdChannel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
@Slf4j   // 打印日志
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {
    @Override
    public ResponseResult findByNameAndPage(ChannelDTO dto) {
        //1: 检查参数
            if(dto==null){
               return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
            }

        // 分页参数查询检查
            dto.checkParam();
        //2:封装查询参数  查询数据
        //按name模糊分页查询
        Page page =new Page(dto.getPage(),dto.getSize());
        //查询参数
        LambdaQueryWrapper<AdChannel> lambdaQueryWrapper= Wrappers.<AdChannel>lambdaQuery();
        //判断dto.getName()不为空
        if(StringUtils.isNotBlank(dto.getName())){
            lambdaQueryWrapper.like(AdChannel::getName,dto.getName());
        }
        //添加排序
        lambdaQueryWrapper.orderByAsc(AdChannel::getOrd);
        //分页参数
        IPage result = page(page, lambdaQueryWrapper);
        //3:结果封装  dto.getPage()第几页
        ResponseResult responseResult=new PageResponseResult(dto.getPage(),dto.getSize(),result.getTotal());
        //MyBatisPuls中调用getRecords()是获取分页结果
        responseResult.setData(result.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult addAdChannel(AdChannel adChannel) {
        //检查参数(名字不为空  长度小于10  名字不能重复)
        if(StringUtils.isBlank(adChannel.getName())||adChannel.getName().length()>10){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称不能为空 或 频道名称长度不能大于10");
        }
        //2: 判断该频道是否存在
        int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                .eq(AdChannel::getName,adChannel.getName())
        );
           if(count>0){
               return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道名称已存在");
           }
           //3: 执行新增
      //新能频道由用户新增 后端只需新增用户新增时的时间
        adChannel.setCreatedTime(new Date());
        save(adChannel);
        //4:返回结果
        return ResponseResult.okResult();
    }

    //修改频道及其状态
    @Override
    public ResponseResult updateAdChanner(AdChannel adChannel) {
        //1:参数校验(id )
        if(adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        //根据id查询原来的频道信息
        AdChannel channel = getById(adChannel.getId());
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"未发现对应频道信息");
        }
        //频道名称  不为空   并且和之前的频道名称不一样  +  判断是否重复的名称
        //校验名称唯一性
        if(StringUtils.isNotBlank(adChannel.getName())
            && !adChannel.getName().equals(channel.getName())
        ){
            //频道名称长度不能大于10
            if(adChannel.getName().length()>10){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道名称长度不能大于10");
            }
            int count = this.count(Wrappers.<AdChannel>lambdaQuery()
                    .eq(AdChannel::getName, channel.getName())
            );
            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST,"该频道已存在");
            }
        }
        //2:执行修改
        this.updateById(adChannel);
        //返回结果
        return ResponseResult.okResult();
    }

    //删除频道功能
    @Override
    public ResponseResult deleteAdChannel(Integer id) {
        //参数校验
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        //2: 判断当前频道是否存在 和   是否有效
        AdChannel channel = getById(id);
        if(channel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"对应的频道数据不存在");
        }
        //判断启用状态下不能删除
        if(channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_ALLOW,"启动状态的频道不可以删除");
        }
        //根据id删除频道
        removeById(id);
        //返回结果
        return ResponseResult.okResult();
    }


}
